﻿using System;
using System.Collections.Generic;
using System.Text;
using YouHP.Utility;
using YouHP.Model.Group;
using YouHP.BLL;
using YouHP.Model;

namespace YouHP.Pages.Group
{

    public class Join : GroupBasePage
    {

        protected new UserInfo User;
        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (ErrorCount > 0) return;

            if (IsPost) HandlePost();
            else HandleGet();
        }

         private new void HandleCommon()
        {

            if (UserData.Id <= 0)
            {
                this.AddErrLine("您还没有登录，请点击登录！");
                return;
            }

            //判断用户与圈子的关系
            if (GroupUserData.Role == Utility.Constants.RoleAdmin || GroupUserData.Role == Utility.Constants.RoleCreator ||
           GroupUserData.Role == Utility.Constants.RoleMember || GroupUserData.Role == Utility.Constants.RoleBlacklist)
            {
                this.AddErrLine("你已是圈中成员！");
                return;
            }

            if (GroupUserData.Role == Utility.Constants.RoleAppling)
            {
                this.AddErrLine("你已经提交了申请,请等待审批！");
                return;
            }

        }
         private void HandlePost()
        {

        }

         protected GroupUserInfo info=new GroupUserInfo();
         private void HandleGet()
        {
            info.GroupId = GroupData.Id;
            info.GroupName = GroupData.GroupName;
            info.JoinTime = DateTime.Now;
            info.PostCount = info.ReplyCount = 0;
            info.UserId = UserData.Id;
            info.UserName = UserData.UserName;

             bool succ = false;
            if (GroupData.JoinPermission == Utility.Constants.JoinOpen)
            {
                if (GroupUserData.Role == Constants.RoleNoExsit)
                {
                    info.Role = Utility.Constants.RoleMember;
                    succ = GroupUserBLL.Instance.AddGroupUser(info);

                }
                else
                {
                    succ = GroupUserBLL.Instance.UpdateGroupUserRole(GroupData.Id, UserData.Id, Constants.RoleMember);
                }


                if (succ)
                {
                    this.AddMsgLine("加入成功！");
                    this.SetUrl("/Group/Group.aspx?gid=" + gid);
                    this.SetMetaRefresh();
                }
                else
                {
                    this.AddErrLine("操作失败！");
                    return;
                }

            }
            else if (GroupData.JoinPermission == Utility.Constants.JoinLimit)
            {

                if (GroupUserData.Role == Constants.RoleNoExsit)
                {
                    info.Role = Utility.Constants.RoleAppling;
                    succ = GroupUserBLL.Instance.AddGroupUser(info);
                }
                else
                {
                    succ = GroupUserBLL.Instance.UpdateGroupUserRole(GroupData.Id, UserData.Id, Constants.RoleAppling);
                }
                if (succ)
                {
                    this.AddMsgLine("你已经向管理员提交声请，请等待批准！");
                    this.SetUrl("/Group/Group.aspx?gid=" + gid);
                    SetMetaRefresh();
                }
                else
                {
                    this.AddErrLine("操作失败！");
                    return;
                }
            }
            else
            {
                AddErrLine("操作失败！");
                return;
            }
        }


    }
}
